package wiki.daos;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

import wiki.banco.Conexao;
import wiki.beans.Noticia;

public class NoticiaDAO
{
  Conexao conexao;
  
  public NoticiaDAO()
  {
    conexao = new Conexao();
  }
  
  protected void finalize()
  {
    conexao.fechar();
  }
  
  public boolean excluir(int idNoticia)
  {
    boolean res = true;
    String query = "DELETE FROM idNoticia "
            + "WHERE idNoticia=?"; // excluir idNoticias quando for igual=?
    conexao.preparar(query);
    try
    {
      conexao.getPstmt().setInt(1, idNoticia);
      res = conexao.executeUpdate();
    } catch (SQLException ex)
    {
      Logger.getLogger(NoticiaDAO.class.getName()).log(Level.SEVERE, null, ex);
    }
    return(res);
  }

public boolean atualizarNoticia(Noticia antiga, Noticia nova)
  {
     boolean res = true;
     String query = "UPDATE idNoticia SET " +
             "titulo=?, " +
             "resumo=?, " +
             "texto=?, " +
             "linkFonte=? " +
             "WHERE idNoticia=?";
     conexao.preparar(query);
    try
    {
      conexao.getPstmt().setString(1, nova.getTitulo());
      conexao.getPstmt().setString(2, nova.getResumo());
      conexao.getPstmt().setString(3, nova.getTexto());
      conexao.getPstmt().setString(4, nova.getLinkFonte());
      conexao.getPstmt().setInt(5, antiga.getIdNoticia());
      
      res = conexao.executeUpdate();
      
    } catch (SQLException ex)
    {
      Logger.getLogger(NoticiaDAO.class.getName()).log(Level.SEVERE, null, ex);
    }
    return(res);
  }
  
  public boolean inserir(Noticia noticia)
  {
    boolean res = false;
    
    String query = "INSERT INTO idNoticia "
            + "(titulo,data, resumo, texto, linkFonte,ativo,codJornalista) VALUES (?,?,?,?,?,?,?)";
    
    conexao.preparar(query);
    try
    {
      conexao.getPstmt().setString(1, noticia.getTitulo());
      conexao.getPstmt().setTimestamp(2, new Timestamp(noticia.getData().getTimeInMillis()));
      conexao.getPstmt().setString(3, noticia.getTexto());
      conexao.getPstmt().setString(4, noticia.getResumo());
      conexao.getPstmt().setString(5, noticia.getLinkFonte());
      conexao.getPstmt().setBoolean(6, noticia.getAtivo());
      conexao.getPstmt().setInt(7, noticia.getCodJornalista());
      
      res = conexao.executeUpdate();
    } catch (SQLException ex)
    {
      Logger.getLogger(NoticiaDAO.class.getName()).log(Level.SEVERE, null, ex);
      res = false;
    }
    return(res);
  }
  
  public Noticia buscar(int idNoticia)
  {
    Noticia res = null;
    
    String query = "SELECT " +
    		"idNoticia, " +
    		"titulo, " +
    		"resumo, " +
    		"texto, " +
    		"linkFonte, " +
    		"DATE_FORMAT(data, '%d/%m/%Y %H:%i') data, " +
    		"ativo, " +
    		"codJornalista " +
            "FROM noticia " +
            "WHERE idNoticia=? ";
    
    conexao.preparar(query);
    
    try
    {
      conexao.getPstmt().setInt(1, idNoticia);
      
      ResultSet rs = conexao.executeQuery();
      
      while (rs.next())
      {
    	  res = new Noticia();
    	  
    	  res.setIdnoticia(rs.getInt("idNoticia"));
    	  res.setTitulo(rs.getString("titulo"));
    	  res.setResumo(rs.getString("resumo"));
    	  res.setTexto(rs.getString("texto"));
    	  res.setLinkFonte(rs.getString("linkFonte"));
    	  res.setData(rs.getString("data"));
    	  res.setAtivo(rs.getBoolean("ativo"));
    	  res.setCodJornalista(rs.getInt("codJornalista"));
      }
      rs.close();
      
    } catch (SQLException ex)
    {
      Logger.getLogger(NoticiaDAO.class.getName()).log(Level.SEVERE, null, ex);
    }
    finally
    {
        return res;
    }
  }
  
  public ArrayList<Noticia> buscarPorTitulo(String titulo){
	  Noticia noticia = null;
	  
	  ArrayList<Noticia> res = new ArrayList<Noticia>();
	  String query = "SELCT * FROM noticia WHERE " +
	  				 "titulo LIKE ? " +
	  				 "ORDER BY idNoticia";
	  
	  conexao.preparar(query);
	  try
	  {
	  	conexao.getPstmt().setString(1, "%"+titulo+"%");
	  	
	  	ResultSet rs = conexao.executeQuery();
	  	while(rs.next()){
	  		noticia = new Noticia();
	    	  
	  		noticia.setIdnoticia(rs.getInt("idNoticia"));
    	  	noticia.setTitulo(rs.getString("titulo"));
    	  	noticia.setResumo(rs.getString("resumo"));
    	  	noticia.setTexto(rs.getString("texto"));
    	  	noticia.setLinkFonte(rs.getString("linkFonte"));
    	  	noticia.setData(rs.getString("data"));
    	  	noticia.setAtivo(rs.getBoolean("ativo"));
    	  	noticia.setCodJornalista(rs.getInt("codJornalista"));
    	  	res.add(noticia);
	  }
	  } catch (SQLException ex) {
		  Logger.getLogger(NoticiaDAO.class.getName()).log(Level.SEVERE, null, ex);
	  }
	  
	  return res;
  }
  
  public ArrayList<Noticia> buscarTodos()
  {
    ArrayList<Noticia> res = new ArrayList<Noticia>();
    String query = "SELECT " +
    		"idNoticia, " +
    		"titulo, " +
    		"resumo, " +
    		"texto, " +
    		"linkFonte, " +
    		"DATE_FORMAT(data, '%d/%m%Y %H:%i') data, " +
    		"ativo, " +
    		"codJornalista" +
            "FROM Noticia " +
            "ORDER BY idNoticia";
    
    conexao.preparar(query);
    try
    {
      ResultSet rs = conexao.executeQuery();
      while (rs.next())
      {
    	  Noticia noticia = new Noticia();
    	  
    	  noticia.setIdnoticia(rs.getInt("idNoticia"));
    	  noticia.setTitulo(rs.getString("titulo"));
    	  noticia.setResumo(rs.getString("resumo"));
    	  noticia.setTexto(rs.getString("texto"));
    	  noticia.setLinkFonte(rs.getString("linkFonte"));
    	  noticia.setData(rs.getString("data"));
    	  noticia.setAtivo(rs.getBoolean("ativo"));
    	  noticia.setCodJornalista(rs.getInt("codJornalista"));
    	  res.add(noticia);
      }
      rs.close();
      
    } catch (SQLException ex)
    {
      Logger.getLogger(NoticiaDAO.class.getName()).log(Level.SEVERE, null, ex);
    }
    return(res);
  }
}
